addEvent("onload", init);
addEvent("onresize", checkOrientation);
var channelmonitoractive = false;
var screenmode = 0;
var label_start_monitor_label = "START MONITOR";
var label_stop_monitor_label = "STOP MONITOR";
var channel_data_initial_value = "INITIAL";
var ChargingPollTimer = null;


function init(){
    try {
        document.getElementById("MonitorToggle").innerHTML = label_start_monitor_label;
        var myStyleTweaks = new StyleTweaker();
        myStyleTweaks.add("Series60/5.0", "styles/tweaks/S605th.css");
        myStyleTweaks.add("Series60/3.2", "styles/tweaks/S603rdFP2.css");
        myStyleTweaks.tweak();
        // Set the timer to update charging channel at three second intervals
        // because it does not support notification style calling
        ChargingPollTimer = setInterval("pollChargingChannel()", 3000);
        initSystemServices();
    } 
    
    catch (e) {
        var error = e.toString();
        alert(error);
    }
}

/*
 Polls for charging channel and if monitoring active, updates widget UI
 */
function pollChargingChannel(){
    try {
        if (channelmonitoractive) {
            displayChargingChannel(GetChargingChannel());
        }
    } 
    catch (e) {
        var error = e.toString();
        alert(error);
    }
}



function checkOrientation(){
    try {
        var width = parseInt(screen.width);
        var height = parseInt(screen.height);
        
        //var landscapeTweaker = new StyleTweaker();
        // Landscape tweaks for template styles not available in templates?
        //landscapeTweaker.add("Series60/5.0", "../resources/styles/tweaks/S605thLandscape.css");
        
        if (width > height) {
        
            // if already in landscape mode, return
            if (screenmode == 1) 
                return;
            screenmode = 1;
            // landscapeTweaker.tweak();
        }
        else {
            // if already in portrait mode, return
            if (screenmode == 0) 
                return;
            screenmode = 0;
            //        landscapeTweaker.untweak();
        }
    } 
    
    catch (e) {
        var error = e.toString();
        alert(error);
    }
}

/*
 Onclik handler for start/stop monitoring button
 Starts and stops channel monitoring
*/
function MonitorToggle_onclick(actionobj){
    try {
        if (channelmonitoractive) {
            actionobj.innerHTML = label_start_monitor_label;
            StopChannelMonitoring()
        }
        else {
            actionobj.innerHTML = label_stop_monitor_label;
            StartChannelMonitoring()
        }
        channelmonitoractive = !channelmonitoractive;
    } 
    catch (e) {
        var error = e.toString();
        alert(error);
    }
}

/*
 Displays charging channel data in widget ui
  {
  "chargingStatus  ": Boolean // Possible values are true (charging ) and false (not charging)
  }
*/

function displayChargingChannel(channelData){
    try {
        document.getElementById("channelCharging").innerHTML = channelData.chargingStatus;
    } 
    catch (e) {
        var error = e.toString();
        alert(error);
    }
}

/*
 Displays battery level channel data in widget ui.
 outPut object is passed to display function in
 service platform asyncronous callback handlers. Schema:
 {
 "batteryLevel  ": Number // Percent charge, 0-100
 }
 */
function displayBatterylevelChannel(outPut){
    try {
        document.getElementById("channelBatterylevel_batteryLevel").innerHTML = outPut.batteryLevel + "%";
    } 
    catch (e) {
        var error = e.toString();
        alert(error);
    }
}

/*
 Displays signal channel data in widget ui.
  outPut object is passed to display function in
 service platform asyncronous callback handlers. Schema:
 {
 "signalStrength  ": Number // Percent signal strength, 0-100
 }
 */
function displaySignalChannel(outPut){
    try {
        //Note : signalStrength is returned interms of decibels by S60. Approximate conversion is made to get the value in percentage.
        document.getElementById("channelSignal_signalStrength").innerHTML = outPut.signalStrength + " %";
    } 
    catch (e) {
        var error = e.toString();
        alert(error);
    }
}

/*
 Displays network channel data in widget ui.
 outPut object is passed to display function in
 service platform asyncronous callback handlers. Schema:
 {
 "networkName  ": String ,// Provides the network name
 "networkStatus": String, // Refer networkStatus for the possible values.
 "networkMode" : String, // Possible values are ModeGsm,ModeCdma,ModeWcdma
 "mobileCountryCode" : String, //Mobile Country Code (MCC)
 "mobileNetworkCode" : String, // Mobile Network Code (MNC)
 "locationStatus" : Boolean, // true  if LAC, CellId are valid, otherwise false
 "areaCode" : Number ,// Location Area Code (LAC) (Currently not supported)
 "cellID" : Number // CellId (Currently not supported)
 }
 */
function displayNetworkChannel(outPut){
    try {

		
        document.getElementById("channelNetwork_networkName").innerHTML = outPut.networkName;
        document.getElementById("channelNetwork_networkStatus").innerHTML = outPut.networkStatus;
        document.getElementById("channelNetwork_networkMode").innerHTML = outPut.networkMode;
        document.getElementById("channelNetwork_mobileCountryCode").innerHTML = outPut.mobileCountryCode;
        document.getElementById("channelNetwork_mobileNetworkCode").innerHTML = outPut.mobileNetworkCode;
        document.getElementById("channelNetwork_locationStatus").innerHTML = outPut.locationStatus;
    } 
    catch (e) {
        var error = e.toString();
        alert(error);
    }
}

/*
This function puts initial label for all channeldata elements in UI
*/
function UIResetChannels() {
	    try {
			
		document.getElementById("channelCharging").innerHTML = channel_data_initial_value;
		document.getElementById("channelSignal_signalStrength").innerHTML = channel_data_initial_value;
        document.getElementById("channelBatterylevel_batteryLevel").innerHTML = channel_data_initial_value;
        document.getElementById("channelNetwork_networkName").innerHTML = channel_data_initial_value;
        document.getElementById("channelNetwork_networkStatus").innerHTML = channel_data_initial_value;
        document.getElementById("channelNetwork_networkMode").innerHTML = channel_data_initial_value;
        document.getElementById("channelNetwork_mobileCountryCode").innerHTML = channel_data_initial_value;
        document.getElementById("channelNetwork_mobileNetworkCode").innerHTML = channel_data_initial_value;
        document.getElementById("channelNetwork_locationStatus").innerHTML = channel_data_initial_value;
    } 
    catch (e) {
        var error = e.toString();
        alert(error);
    }
}
	


